package cn.tac.test.seo.rpc.provider.controller;

import cn.tac.test.seo.rpc.core.pojo.Foo;
import cn.tac.test.seo.rpc.core.pojo.FooQuery;
import cn.tac.test.seo.rpc.core.pojo.FullResult;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author tac
 * @since 20/12/2017
 */
@RestController
@RequestMapping("foo")
public class FooController {
    private static Map<String, Foo> foos = Maps.newHashMap();

    static {
        foos.put("1", new Foo("hh", 12, true, null));
        foos.put("2", new Foo("oo", 22, false, null));
        foos.put("3", new Foo("aa", 32, false, new Date()));
    }

    @GetMapping("/{id}")
    public Foo get(@PathVariable("id") String id) {
        return foos.get(id);
    }

    @GetMapping("full/{id}")
    public FullResult<Foo> fullGet(@PathVariable("id") String id) {
        return new FullResult<>(id, foos.get(id));
    }


    @GetMapping("query")
    public List<Foo> query(@ModelAttribute FooQuery query) {
        List<Foo> ls = new ArrayList<>();
        foos.forEach((key, val) -> {
            if (StringUtils.isNotBlank(query.getBar1()) && val.getBar1().equals(query.getBar1())) {
                ls.add(val);
            }
        });
        return ls;
    }

    @GetMapping("/listAll")
    public List<Foo> listAll() {
        return Lists.newArrayList(foos.values());
    }

    @PutMapping
    public String create(@RequestBody Foo foo) {
        String id = UUID.randomUUID().toString().replace("-", "");
        foos.put(id, foo);
        return id;
    }
}
